package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.domain.Member;
import com.itheima.domain.Order;
import com.itheima.domain.OrderSetting;
import com.itheima.entity.Result;
import com.itheima.service.OrderServie;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderServie.class)
@Transactional
public class OrderServiceImpl implements OrderServie {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;

    @Override
    public Result order(Map map) throws Exception {
        //1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);//预约日期
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
        if (orderSetting.getNumber() == orderSetting.getReservations()) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
        String telephone = (String) map.get("telephone");//获取页面输入的手机号
        Member member = memberDao.findByTel(telephone);//根据手机号去查会员表,是否是会员
        if (member != null) {
            //有可能重复预约
            Integer memberId = member.getId();//会员id
            Integer setmealId = Integer.parseInt((String) map.get("setmealId"));//预约的套餐id
            Order order = new Order(memberId, date, setmealId);
            List<Order> orderList = orderDao.findByCondition(order);
            if (orderList != null && orderList.size() > 0) {
                //说明用户在重复预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else {
            //表示该用户不是会员,自动完成注册并进行预约
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber((String) map.get("telephone"));
            member.setSex((String) map.get("sex"));
            member.setIdCard((String) map.get("idCard"));
            member.setRegTime(new Date());
            memberDao.add(member);//自动完成会员注册
        }
        //4、预约成功，将预约信息存入预约表
        Order order = new Order();
        order.setMemberId(member.getId());//设置会员id
        order.setOrderDate(date);//预约时间
        order.setOrderType((String) map.get("orderType"));//设置预约方式
        order.setOrderStatus(Order.ORDERSTATUS_NO);//就诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐id
        if (map.containsKey("addressId")) {
            order.setAddressId(Integer.parseInt((String) map.get("addressId")));//就诊地址id
        }
        if (map.containsKey("address")) {
            String address = (String) map.get("address");
            Map addressMap = orderDao.findIdByAddress(address);
            if (addressMap != null && addressMap.size() > 0) {
                order.setAddressId((Integer) addressMap.get("id"));
            }
        }

        orderDao.add(order);
        //5.更新当日的已预约人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);


        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
        //通过id查询预约表
        Map map = orderDao.findById4Data(id);
        if (map != null) {
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate", DateUtils.parseDate2String(orderDate));
        }

        return map;
    }
}
